import subprocess
import os

# 指定输出文件路径
output_path = r"E:\舆情分析\gitee\public-sentiment-monitoring\fastapi\requirements.txt"

# 使用pip freeze命令生成requirements.txt
result = subprocess.run(['pip', 'freeze'], stdout=subprocess.PIPE, text=True)

# 将结果写入文件
with open(output_path, 'w', encoding='utf-8') as f:
    f.write(result.stdout)

print(f"所有包已成功写入到 {output_path}")